//
//  MapViewController.m
//  小豌旅行
//
//  Created by wyzcWLB on 16/9/27.
//  Copyright © 2016年 wyzcWLB. All rights reserved.
//

#import "MapViewController.h"
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>

@interface MapViewController ()<CLLocationManagerDelegate,MKMapViewDelegate>
//定义定位管理者
@property(nonatomic,strong)CLLocationManager *locationManager;
@property(nonatomic,strong)MKMapView *mapView;
@end

@implementation MapViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    /**创建地图*/
    [self creatMap];
    /**定位当前*/
    [self selfLocation];
    
    //创建左边那个返回按钮
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 10, 50, 50);
    [button setImage:[UIImage imageNamed:@"login_close_icon"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(btnLeftClick) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:button];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

#pragma mark 定位自己
-(void)selfLocation{
    //创建定位对象
    _locationManager =[[CLLocationManager alloc] init];
    //设置定位属性
    _locationManager.desiredAccuracy =kCLLocationAccuracyBest;
    
    //设置定位跟新距 militer
    _locationManager.distanceFilter=10.0;
    //绑定定位委托
    _locationManager.delegate=self;
    /**设置用户请求服务*/
    //1.去info.plist文件添加定位服务描述
    
    //取出当前应用的定位服务状态(枚举值)
    CLAuthorizationStatus status =[CLLocationManager authorizationStatus];
    
    //如果未授权 则请求
    if (status==kCLAuthorizationStatusNotDetermined) {
        
        [_locationManager requestAlwaysAuthorization];
    }
    //开始定位
    [_locationManager startUpdatingLocation];
}
#pragma mark CLLocationManagerDelegate
//定位后的返回结果
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    //locations数组中存储的是CLLocation
    //遍历数组 取出坐标 --》如果不需要 也可以不遍历
    CLLocation *location =[locations firstObject];
    
    CLGeocoder *geocoder=[[CLGeocoder alloc]init];
    [geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        //CLPlacemark是地标
        CLPlacemark *mark=[placemarks firstObject];

        //设置地图显示该经纬度的位置
        MKCoordinateRegion region =MKCoordinateRegionMake(location.coordinate, MKCoordinateSpanMake(0.01, 0.01));
        [_mapView setRegion:region animated:YES];
        // 创建大头针
        MKPointAnnotation * pointAnnotation = [[MKPointAnnotation alloc] init];
        // 设置经纬度
        pointAnnotation.coordinate = location.coordinate;
        
        // 设置主标题
        pointAnnotation.title = mark.name;
        
        // 将大头针添加到地图上
        [_mapView addAnnotation:pointAnnotation];

    }];

}
#pragma mark 创建地图
-(void)creatMap{
    
    //实例化
    _mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
    //绑定委托
    _mapView.delegate=self;
    //添加到界面
    [self.view addSubview:_mapView];

    NSArray *mapTypeArray = @[@"标准",@"卫星",@"混合"];
    UISegmentedControl *segment =[[UISegmentedControl alloc] initWithItems:mapTypeArray];
    segment.frame=CGRectMake(65, 20, Screen_Width*0.7, 50);
    [_mapView addSubview:segment];
    segment.selectedSegmentIndex=0;
    //添加UISegmentedControl的事件响应方法
    [segment addTarget:self action:@selector(mapTypeChanged:) forControlEvents:UIControlEventValueChanged];
    
}
-(void)btnLeftClick
{
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [self dismissViewControllerAnimated:YES completion:nil];
}

//segment响应回调
-(void)mapTypeChanged:(UISegmentedControl *)sender{
    
    //获得当前segment索引
    NSInteger index =sender.selectedSegmentIndex;
    
    //设置地图的显示方式
    
    _mapView.mapType =index;
}
@end